org.mentalsmash.asksusanna.gui.GUI = function(conf){
	
	var container = $(conf.containerSelector);
	
	function rgb2hex(R,G,B) {
		function toHex(N) {
		 	if (N==null) {
				return "00";
			}
		 	
			N = parseInt(N);
			
			if ( N == 0 || isNaN(N) ) {
				return "00";
			}
		 	
			N = Math.max(0,N);
			N = Math.min(N,255);
			N = Math.round(N);
			
		 	return "0123456789ABCDEF".charAt((N-N%16)/16)
		      + "0123456789ABCDEF".charAt(N%16);
		}
		
		return toHex(R)+toHex(G)+toHex(B);
	}
	
	function hsl2rgb(h, s, l){
	    var r, g, b;

	    if(s == 0){
	        r = g = b = l; // achromatic
	    }else{
	        function hue2rgb(p, q, t){
	            if(t < 0) t += 1;
	            if(t > 1) t -= 1;
	            if(t < 1/6) return p + (q - p) * 6 * t;
	            if(t < 1/2) return q;
	            if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
	            return p;
	        }

	        var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
	        var p = 2 * l - q;
	        r = hue2rgb(p, q, h + 1/3);
	        g = hue2rgb(p, q, h);
	        b = hue2rgb(p, q, h - 1/3);
	    }

	    return [r * 255, g * 255, b * 255];
	}
	
	/**
	 *  returns a function that maps an input number x
	 *  to a point on a line that has something to do
	 *  with the max and minimum score... (the function
	 *  maps a word's score to an L valure for HSL)
	 **/
	function mappingFunction(phrase) {
		var min = 0, max = 0;

		$(phrase.getWords()).each(function(i,word){
			var score = word.normalizedScore();
			if (score < min) {
				min = score;
			}
			if (score > max) {
				max = score;
			}
		});

		return function(x){
			//line between two points : [min,0],[max,1];
			return (x - min)/(max - min);
		};
	}
	
	return {
		'showPhraseResults' : function(phrase) {
			var box = this.buildPhraseBox(phrase);
			box.hide();
			if ($('#results').length >= 0) {
				$('#results').fadeOut(function(){
					$('#results').remove();
					$('#main').append(box);
					box.fadeIn();
				});
			} else {
				$('#main').append(box);
				box.fadeIn();
			}
			
			var dbugger = new org.mentalsmash.asksusanna.gui.Debugger(phrase,conf);
			var dbugBox = dbugger.getDebugBox();
			dbugBox.hide();
			
			if ($('#debugger').length > 0){
				$('#debugger').fadeOut(function(){
					$('#debugger').remove();
					$('body').append(dbugBox);
					dbugBox.fadeIn();
				});
			} else {
				$('body').append(dbugBox);
				dbugBox.fadeIn();
			}
			
			
		},
		'buildPhraseBox' : function(phrase) {
			var gui = this;
			var mapper = mappingFunction(phrase);
			
			var phraseBox = $('<div id="results"></div>');
			phraseBox.addClass("phrase-results");

			$(phrase.getWords()).each(function(i,word){
				var lValue = mapper(word.normalizedScore());
				var rgb = hsl2rgb(1,1,(lValue*0.5)+ 0.4);
				var hex = rgb2hex(rgb[0],rgb[1],rgb[2]);
				var color = '#'+hex;

				var span = $('<p></p>');
				span.text('');

				var roundScore = Math.round(word.normalizedScore() * Math.pow(10,2)) / Math.pow(10,2);

				var scoreSpan = $('<span class="score"></span>').text(roundScore).css('background-color',color);
				var wordSpan = $('<span class="word"></span>').text(word.getWord());

				span.append(scoreSpan);
				span.append(wordSpan);
				
				var tooltip = new org.mentalsmash.asksusanna.gui.Tooltip(phrase,span,word,color);
				
				span.hover(function(){
					var holder = $('#tooltip-holder');
					
					if (holder.length == 0) {
						holder = $('<div id="tooltip-holder"></div>');
						$('body').append(holder);
					}
					
					holder.empty();
					tooltip.hide();
					holder.append(tooltip);
					tooltip.fadeIn();
				}, function(){
					tooltip.fadeOut(function(){
						tooltip.remove();
					});
				})
				
				phraseBox.append(span);
				
			});
					
			return phraseBox;
		}
	}
	
};